﻿hpxAdminApp.controller('billStoreController', function (exportService, $rootScope,$timeout, $interval, $scope, $stateParams, $state, API_URL, NgTableParams, billService, constantsService, addressService, EXPORT_URL) {
    $scope.filter = {
        bill_type_id: '',
        accept_type_id: '',
        accept_type_ids: [],
        bill_sum_price: '',
        deadline_time: '',
        bidding_mode: '',
        bill_status: '',
        bill_flaws: '',

        trade_appoint_day: '',
        is_bidding: '',
        billType: '',
        is_bid_clo: 0,
        bill_sum_price_min: '',
        bill_sum_price_max: '',
        biddingType: '',
        deadline_time_max: '',
        deadline_time_min: '',
        acceptType: '',
        isShowArea: false,
        product_location_id: '',
        showTips: false,
        isFind: 0,
        showHelp: false,
        show_price: true,
        showBid: true,
        bill_sum_price_type: '',
        acceptor_name: '',
        isShowAcceptor: false
    }
    $scope.filters = {
        showChoose: true,
        showPriceEm: true,
        showDeteEm: true,
        showQuick: false,
        showPrice1: false,
        showDete1: false,
        showPushTime: false, //发布时间
        showPushTimeEm: true,
    }
    $scope.model = {
        bill_type_id: '',
        bill_sum_price_min: '',
        bill_sum_price_max: '',
        deadline_time_max: '',
        deadline_time_min: ''
    }
    // 输入承兑人筛选
    $scope.changeAcceptorName = function () {
        $scope.filter.isShowAcceptor = true
    }
    // 匹配票查查
    $scope.goToPcc = function (item) {
        //console.log(item)
        $state.go('app.constants.pccMain', { 'bill_sum_price': item.bill_sum_price, 'dead_date': item.bill_deadline_time, 'bill_flaws': item.bill_flaw_ids, 'accept_type_ids': item.acceptor_type_id });
    }
    // 如果输入框中有数据，则选择的地方就清空
    $scope.getPrice1 = function () {
        $scope.filter.bill_sum_price_type = 10;
        if ($scope.model.bill_sum_price_min && $scope.model.bill_sum_price_min != '' || $scope.model.bill_sum_price_max && $scope.model.bill_sum_price_max != '') {
            //$scope.filter.bill_sum_price_min = '';
            //$scope.filter.bill_sum_price_max = '';
            //$scope.filter.bill_sum_price = ''
            $scope.filter.bill_sum_price = 'custom';
        }
        //if ($scope.model.deadline_time_min && $scope.model.bill_sum_price_min != '' || $scope.model.deadline_time_max && $scope.model.deadline_time_max != '') {
        //    $scope.filter.deadline_time_min = '';
        //    $scope.filter.deadline_time_max = '';
        //    $scope.filter.deadline_time = ''
        //}
    }
    $scope.getDate1 = function () {
        if ($scope.model.deadline_time_min && $scope.model.deadline_time_min != '' || $scope.model.deadline_time_max && $scope.model.deadline_time_max != '') {
            //$scope.filter.deadline_time_min = '';
            //$scope.filter.deadline_time_max = '';
            $scope.filter.deadline_time = 'custom';
        }
    }

    $scope.getPriceDate = function () {
        if ($scope.model.bill_sum_price_min || $scope.model.bill_sum_price_min != '' || $scope.model.bill_sum_price_max || $scope.model.bill_sum_price_max != '') {
            $scope.filter.bill_sum_price_min = $scope.model.bill_sum_price_min;
            $scope.filter.bill_sum_price_max = $scope.model.bill_sum_price_max;
        } else {
            $scope.filter.bill_sum_price_min = $scope.filter.bill_sum_price_min;
            $scope.filter.bill_sum_price_max = $scope.filter.bill_sum_price_max;
        }

        if ($scope.model.deadline_time_min || $scope.model.deadline_time_min != '' || $scope.model.deadline_time_max || $scope.model.deadline_time_max != '') {
            $scope.filter.deadline_time_min = $scope.model.deadline_time_min;
            $scope.filter.deadline_time_max = $scope.model.deadline_time_max;
        } else {
            $scope.filter.deadline_time_min = $scope.filter.deadline_time_min;
            $scope.filter.deadline_time_max = $scope.filter.deadline_time_max;
        }
    }

    $scope.po_enter = function (item) {
        item.active = true;
    }
    $scope.po_leave = function (item) {
        item.active = false;
    }
    //提取数字
    function extractNum(str) {
        str = str.replace(/[\u4E00-\u9FA5]/g, "")
        str = str.replace('(', ''),
        str = str.replace(')', '');
        var str_before = str.split("-")[0];
        var str_after = str.split("-")[1];
        return [str_before, str_after];
    }
    if ($stateParams.id) {
        $scope.model.bill_type_id = $stateParams.id;
        $scope.filter.billType = $stateParams.id;
    } else {
        $scope.filter.billType = '';
    }

    $scope.registerLogin = function (re_lo_id) {
        if (re_lo_id == 1) {
            $state.go("app.loginInfo");
        } else {
            $state.go("app.signup");
        }
    }
    // 修改票据商城
    // 选择票据类型
    $scope.billTypeAllChange = function () {
        $scope.filter.bill_type_id = '';
    }
    $scope.billTypeChange = function (bill_types) {
        $scope.filter.bill_type_id = bill_types;
    }
    // 选择承兑类型
    $scope.acceptTypeAllChange = function () {
        $scope.filter.accept_type_id = '';
        $scope.filter.accept_type_ids = [];
    }
    $scope.acceptTypeChange = function (acceptTypeId) {
        //$scope.filter.accept_type_id = acceptTypeId;

        if ($scope.filter.accept_type_ids.indexOf(acceptTypeId) == -1) {
            $scope.filter.accept_type_ids.push(acceptTypeId);
        } else {
            var index = $scope.filter.accept_type_ids.indexOf(acceptTypeId);
            if (index > -1) {
                $scope.filter.accept_type_ids.splice(index, 1);
            }
        }

        if ($scope.filter.accept_type_ids.length == 7) {
            $timeout(function () {
                $scope.filter.accept_type_id = '';
                $scope.filter.accept_type_ids = [];
            }, 200);
        } else {
            $scope.filter.accept_type_ids = $scope.filter.accept_type_ids;
        }
    }
    // 选择票面金额
    $scope.priceTypeAllChange = function () {
        $scope.filter.bill_sum_price_type = '';
        $scope.filter.bill_sum_price = '';
        $scope.filter.bill_sum_price_min = '';
        $scope.filter.bill_sum_price_max = '';
        $scope.model.bill_sum_price_min = '';
        $scope.model.bill_sum_price_max = '';
    }
    $scope.priceTypeChange = function (price) {
        $scope.filter.bill_sum_price_type = '';
        $scope.filter.bill_sum_price = price;
        $scope.model.bill_sum_price_min = '';
        $scope.model.bill_sum_price_max = '';
        if (price == 10) {
            $scope.filter.bill_sum_price_min = 0;
            $scope.filter.bill_sum_price_max = 10;
        } else if (price == 50) {
            $scope.filter.bill_sum_price_min = 10;
            $scope.filter.bill_sum_price_max = 50;
        } else if (price == 100) {
            $scope.filter.bill_sum_price_min = 50;
            $scope.filter.bill_sum_price_max = 100;
        } else if (price == 101) {
            $scope.filter.bill_sum_price_min = 100;
            $scope.filter.bill_sum_price_max = '';
        }
    }
    // 选择到期期限
    $scope.deadlineTimeTypeAllChange = function () {
        $scope.filter.deadline_time = '';
        $scope.filter.deadline_time_min = '';
        $scope.filter.deadline_time_max = '';
        $scope.model.deadline_time_min = '';
        $scope.model.deadline_time_max = '';
    }
    $scope.deadlineTimeTypeChange = function (deadlineTime) {
        $scope.filter.deadline_time = deadlineTime;
        $scope.model.deadline_time_min = '';
        $scope.model.deadline_time_max = '';
        if (deadlineTime == 90) {
            $scope.filter.deadline_time_min = 0;
            $scope.filter.deadline_time_max = 90;
        } else if (deadlineTime == 150) {
            $scope.filter.deadline_time_min = 90;
            $scope.filter.deadline_time_max = 150;
        } else if (deadlineTime == 185) {
            $scope.filter.deadline_time_min = 150;
            $scope.filter.deadline_time_max = 185;
        } else if (deadlineTime == 330) {
            $scope.filter.deadline_time_min = 185;
            $scope.filter.deadline_time_max = 330;
        } else if (deadlineTime == 331) {
            $scope.filter.deadline_time_min = 330;
            $scope.filter.deadline_time_max = '';
        }
    }
    //选择竞价方式
    $scope.biddingTypeAllChange = function () {
        $scope.filter.bidding_mode = '';
        $scope.filter.show_price = true;
    }
    $scope.biddingTypeChange = function (biddingId) {
        $scope.filter.bidding_mode = biddingId;
        if (biddingId == 3212) {
            $scope.filter.show_price = false;
        } else {
            $scope.filter.show_price = true;
        }
    }
    // 选择票据状态
    $scope.billStatusTypeAllChange = function () {
        $scope.filter.bill_status = '';
    }
    $scope.billStatusTypeChange = function (bill_status_id) {
        $scope.filter.bill_status = bill_status_id;
    }
    // 瑕疵
    $scope.flawTypeAllChange = function () {
        $scope.filter.bill_flaws = ''
    }
    $scope.flawTypeChange = function (flawId) {
        $scope.filter.bill_flaws = flawId;
    }
    // 鼠标移入移除问号
    $scope.mouse_enter = function () {
        $scope.filter.showHelp = true;
    }
    $scope.mouse_leave = function () {
        $scope.filter.showHelp = false;
    }
    // 获取所有人的公开的和发布的票
    if (sessionStorage.getItem('getFilter')) {
        $scope.filter = JSON.parse(sessionStorage.getItem('getFilter'));
        $scope.priceTypeChange($scope.filter.bill_sum_price)
        $scope.deadlineTimeTypeChange($scope.filter.deadline_time);
        $timeout(function () {
            $scope.submitFilter();
        })
    } else {
        $scope.filter = $scope.filter;
    }
    $scope.getModel = JSON.parse(sessionStorage.getItem('getModel'));
    if ($scope.getModel && $scope.getModel != '') {
        if ($scope.getModel.bill_sum_price_min != "" || $scope.getModel.bill_sum_price_max != "") {
            $scope.model.bill_sum_price_min = $scope.getModel.bill_sum_price_min;
            $scope.model.bill_sum_price_max = $scope.getModel.bill_sum_price_max;
            $scope.getPriceDate();
        }
        if ($scope.getModel.deadline_time_min != "" || $scope.getModel.deadline_time_max != "") {
            $scope.model.deadline_time_min = $scope.getModel.deadline_time_min;
            $scope.model.deadline_time_max = $scope.getModel.deadline_time_max;
            $scope.getPriceDate();
        }
    }

    $scope.tableParams = new NgTableParams({ 'sorting': { 'bill_product_create_time': 'desc' } }, {
        getData: function (params) {
            return billService.getBillAllProduct(params, $scope.filter).then(function (data) {
                $scope.first = $scope.getFirst(params);
                return data;
            });
        }
    });

    // 票面金额排序
    $scope.priceSort = function () {
        $scope.filters.showPrice1 = true;
        $scope.filters.showDete1 = false;
        $scope.filters.showQuick = false;
        $scope.filters.showPushTime = false;

        $scope.filters.showDeteEm = true;
        $scope.filters.showChoose = true;
        $scope.filters.showPushTimeEm = true;

        $scope.getPriceDate();
        if (!$scope.filters.showPriceEm) {
            $scope.filters.showPriceEm = true;
            $scope.tableParams = new NgTableParams({ 'sorting': { 'bill_sum_price': 'asc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                        $scope.statusCode = data;
                        if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                            if (!data || data == '') {
                                $scope.filter.showTips = true;
                            } else {
                                $scope.filter.showTips = false;
                                $scope.first = $scope.getFirst(params);
                                return data;
                            }
                        }
                    });
                }
            });
            //$scope.tableParams.reload();
        } else {
            $scope.filters.showPriceEm = false;
            $scope.tableParams = new NgTableParams({ 'sorting': { 'bill_sum_price': 'desc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                        $scope.statusCode = data;
                        if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                            if (!data || data == '') {
                                $scope.filter.showTips = true;
                            } else {
                                $scope.filter.showTips = false;
                                $scope.first = $scope.getFirst(params);
                                return data;
                            }
                        }
                    });
                }
            });
            // $scope.tableParams.reload();
        }
    }

    // 到期天数排序
    $scope.dateSort = function () {
        $scope.filters.showDete1 = true;
        $scope.filters.showPrice1 = false;
        $scope.filters.showQuick = false;
        $scope.filters.showPushTime = false;

        $scope.filters.showPriceEm = true;
        $scope.filters.showChoose = true;
        $scope.filters.showPushTimeEm = true;

        $scope.getPriceDate();
        if (!$scope.filters.showDeteEm) {
            $scope.filters.showDeteEm = true;
            $scope.tableParams = new NgTableParams({ 'sorting': { 'bill_deadline_time': 'asc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                        $scope.statusCode = data;
                        if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                            if (!data || data == '') {
                                $scope.filter.showTips = true;
                            } else {
                                $scope.filter.showTips = false;
                                $scope.first = $scope.getFirst(params);
                                return data;
                            }
                        }
                    });
                }
            });
        } else {
            $scope.filters.showDeteEm = false;
            $scope.tableParams = new NgTableParams({ 'sorting': { 'bill_deadline_time': 'desc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                        $scope.statusCode = data;
                        if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                            if (!data || data == '') {
                                $scope.filter.showTips = true;
                            } else {
                                $scope.filter.showTips = false;
                                $scope.first = $scope.getFirst(params);
                                return data;
                            }
                        }
                    });
                }
            });
        }
    }

    // 买卖差额排序按钮
    $scope.enerySort = function () {
        $scope.filters.showQuick = true;
        $scope.filters.showDete1 = false;
        $scope.filters.showPrice1 = false;
        $scope.filters.showPushTime = false;

        $scope.filters.showPriceEm = true;
        $scope.filters.showDeteEm = true;
        $scope.filters.showPushTimeEm = true;

        $scope.getPriceDate();
        if (!$scope.filters.showChoose) {
            $scope.filters.showChoose = true;
            $scope.tableParams = new NgTableParams({ 'sorting': { 'billOfferEveryPlus': 'asc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                        $scope.statusCode = data;
                        if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                            if (!data || data == '') {
                                $scope.filter.showTips = true;
                            } else {
                                $scope.filter.showTips = false;
                                $scope.first = $scope.getFirst(params);
                                return data;
                            }
                        }
                    });
                }
            });
        } else {
            $scope.filters.showChoose = false;
            $scope.tableParams = new NgTableParams({ 'sorting': { 'billOfferEveryPlus': 'desc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                        $scope.statusCode = data;
                        if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                            if (!data || data == '') {
                                $scope.filter.showTips = true;
                            } else {
                                $scope.filter.showTips = false;
                                $scope.first = $scope.getFirst(params);
                                return data;
                            }
                        }
                    });
                }
            });
        }
    }

    //按发布时间排序
    $scope.pushTimeSort = function () {
        $scope.filters.showPushTime = true;
        $scope.filters.showDete1 = false;
        $scope.filters.showPrice1 = false;
        $scope.filters.showQuick = false;

        $scope.filters.showPriceEm = true;
        $scope.filters.showDeteEm = true;
        $scope.filters.showChoose = true;

        $scope.getPriceDate();
        if (!$scope.filters.showPushTimeEm) {
            $scope.filters.showPushTimeEm = true;
            $scope.tableParams = new NgTableParams({ 'sorting': { 'publishing_time': 'asc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                        $scope.statusCode = data;
                        if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                            if (!data || data == '') {
                                $scope.filter.showTips = true;
                            } else {
                                $scope.filter.showTips = false;
                                $scope.first = $scope.getFirst(params);
                                return data;
                            }
                        }
                    });
                }
            });
        } else {
            $scope.filters.showPushTimeEm = false;
            $scope.tableParams = new NgTableParams({ 'sorting': { 'publishing_time': 'desc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                        $scope.statusCode = data;
                        if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                            if (!data || data == '') {
                                $scope.filter.showTips = true;
                            } else {
                                $scope.filter.showTips = false;
                                $scope.first = $scope.getFirst(params);
                                return data;
                            }
                        }
                    });
                }
            });
        }
    }
    //筛选
    $scope.submitFilter = function () {
        // 点击筛选，存储筛选条件
        var str1 = JSON.stringify($scope.filter);
        sessionStorage.setItem('getFilter', str1);
        sessionStorage.getItem('getFilter');
        var str_model = {
            'bill_sum_price_min': $scope.model.bill_sum_price_min,
            'bill_sum_price_max': $scope.model.bill_sum_price_max,
            'deadline_time_min': $scope.model.deadline_time_min,
            'deadline_time_max': $scope.model.deadline_time_max
        }
        var str_model1 = JSON.stringify(str_model);
        sessionStorage.setItem('getModel', str_model1);
        sessionStorage.getItem('getModel');
        if ($scope.filter.offer_rate && $scope.filter.offer_rate != '' && $scope.filter.offer_rate != undefined || $scope.filter.offer_every_plus && $scope.filter.offer_every_plus != '' && $scope.filter.offer_every_plus != undefined) {
            $scope.filter.showBid = false;
        } else {
            $scope.filter.showBid = true;
        }
        //$scope.filter.showBid = false;
        $scope.filter.showQuick = false;
        $scope.getPriceDate();
        // 登录成功之后，在设置一下seeionStore
        var str11 = JSON.stringify($scope.filter);
        sessionStorage.setItem('getFilter', str11);
        sessionStorage.getItem('getFilter');
        $scope.tableParams = new NgTableParams({ 'sorting': { 'publishing_time': 'desc' } }, {
            getData: function (params) {
                var p1 = params.page();
                return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                    $scope.statusCode = data;
                    if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                        if (!data || data == '') {
                            $scope.filter.showTips = true;
                        } else {
                            $scope.filter.showTips = false;
                            $scope.first = $scope.getFirst(params);
                            return data;
                        }
                    }
                });
            }
        });
        //如果输入了买方价格,调用买卖差额排序
        if ($scope.filter.offer_rate != '' && $scope.filter.offer_rate != undefined || $scope.filter.offer_every_plus != '' && $scope.filter.offer_every_plus != undefined) {
            $scope.filters.showChoose = true;
            $scope.enerySort();
        }
    }
    //获取排序方式缓存
    if (sessionStorage.getItem('sortStatus')) {
        var sortStatus = JSON.parse(sessionStorage.getItem('sortStatus'));
        //票面金额
        if (sortStatus.showPrice1 && sortStatus.showPriceEm) {
            $scope.filters.showPriceEm = false;
            $scope.priceSort();
        } else if (sortStatus.showPrice1 && !sortStatus.showPriceEm) {
            $scope.filters.showPriceEm = true;
            $scope.priceSort();
        }
        //到期日
        if (sortStatus.showDete1 && sortStatus.showDeteEm) {
            $scope.filters.showDeteEm = false;
            $scope.dateSort();
        } else if (sortStatus.showDete1 && !sortStatus.showDeteEm) {
            $scope.filters.showDeteEm = true;
            $scope.dateSort();
        }
        //差额
        if (sortStatus.showQuick && sortStatus.showChoose) {
            $scope.filters.showChoose = false;
            $scope.enerySort();
        } else if (sortStatus.showQuick && !sortStatus.showChoose) {
            $scope.filters.showChoose = true;
            $scope.enerySort();
        }
        //发布日期
        if (sortStatus.showPushTime && sortStatus.showPushTimeEm) {
            $scope.filters.showPushTimeEm = false;
            $scope.pushTimeSort();
        } else if (sortStatus.showPushTime && !sortStatus.showPushTimeEm) {
            $scope.filters.showPushTimeEm = true;
            $scope.pushTimeSort();
        }
    }
    //清空筛选
    $scope.deleteFliter = function () {
        $scope.filter = {
            bill_type_id: '',
            accept_type_id: '',
            accept_type_ids: [],
            bill_sum_price: '',
            deadline_time: '',
            bidding_mode: '',
            bill_status: '',
            bill_flaws: '',

            trade_appoint_day: '',
            is_bidding: '',
            billType: '',
            is_bid_clo: 0,
            bill_sum_price_min: '',
            bill_sum_price_max: '',
            biddingType: '',
            deadline_time_max: '',
            deadline_time_min: '',
            acceptType: '',
            isShowArea: false,
            product_location_id: '',
            showTips: false,
            isFind: 0,
            showHelp: false,
            show_price: true,
            showBid: true
        }
        $scope.model.bill_sum_price_min = '';
        $scope.model.bill_sum_price_max = '';
        $scope.model.deadline_time_min = '';
        $scope.model.deadline_time_max = '';
        // 清空seeion中的额筛选条件
        $timeout(function () {
            sessionStorage.removeItem("getFilter");
            sessionStorage.removeItem("getModel");
        }, 100);
        //$scope.tableParams.reload();
        $scope.tableParams = new NgTableParams({ 'sorting': { 'publishing_time': 'desc' } }, {
            getData: function (params) {
                var p1 = params.page();
                return billService.getBillAllProductes(params, $scope.filter, p1).then(function (data) {
                    $scope.statusCode = data;
                    if ($scope.statusCode != 'NotBeginning' && $scope.statusCode != 'Close') {
                        if (!data || data == '') {
                            $scope.filter.showTips = true;
                        } else {
                            $scope.filter.showTips = false;
                            $scope.first = $scope.getFirst(params);
                            return data;
                        }
                    }
                });
            }
        });
    }


    //刷新
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }
    

});
